package com.zpark.humpro.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * 数据库连接类
 */
public class JdbcUtil {

    //数据源初始化
    private static DataSource dataSource;

    //只运行一次，用静态代码块
    static {

        try {
            //  加载配置文件
            Properties prop = new Properties();
            prop.load(JdbcUtil.class.getClassLoader().getResourceAsStream("druid.properties"));


            //获取数据库连接池对象
            dataSource = DruidDataSourceFactory.createDataSource(prop);

        }catch (Exception e){
            //将错误信息记录日志
            e.printStackTrace();

        }


    }

    //获取数据库连接
    public static Connection getConnection(){
        try {

            return dataSource.getConnection();
        }catch (SQLException e){
            throw new RuntimeException(e);

        }



    }




}
